<template>
  <div>
    <div class="header">
    <van-nav-bar
      title="账号登录"
      left-arrow
      @click-left="onClickLeft"
    />
    </div>
    <!--使用组件van-form 需要在main.js中引入Form-->
    <van-form @submit="onSubmit">
    <!--使用组件van-field 需要在main.js中引入Field  -->
  <van-field
    v-model="username"
    name="username"
    placeholder="请输入账号"
    :rules="[
    { required: true, message: '' },
    { pattern: /^\w{3,}/, message:'用户名最少6位哦'}
    ]"
  />
  <van-field
    v-model="password"
    name="password"
    type="password"
    placeholder="请输入密码"
    :rules="[
    { required: true, message: '' },
    { pattern: /^\w{3,}/, message:'密码最少6位哦'}
    ]"
  />
  <div style="margin: 16px;">
    <!--使用组件van-button  需要在main.js中引入Button -->
    <van-button  block type="info" native-type="submit">登录</van-button>
  </div>
</van-form>
 <router-link class="link" to="/register">还没有账号，去注册~</router-link>
  </div>
</template>

<script>

// 引入登录接口
import { login } from '@/api/user'
// 引入 setToken
import { setToken } from '@/utils/storage'

export default {
  name: 'HkzfVantLogin',

  data () {
    return {
      username: '',
      password: ''
    }
  },
  methods: {
    async onSubmit (values) {
      // console.log('submit', values)
      const res = await login(values)
      // console.log(res)
      // 将res中的token存入本地
      setToken(res.body.token)
      this.$toast.success('登录成功')
      setTimeout(() => {
        this.$router.push('/my')
      }, 500)

      // console.log(values)
    },
    onClickLeft () {
      // 点击 “<” 跳转到my.vue
      this.$router.push('/my')
    }
  }
}
</script>

<style lang="less" scoped>
.nav-bar {
  background-color:#21b97a;
  color: #fa6d1d;
}
.link {
  color: #069;
  font-size: 12px;
  padding-left: 20px;
  float: left;
}

</style>
